<?php
/**
 * Author: Hancock
 * Email: 84622365@qq.com
 * Date: 3/23/2016
 * Des: 测试控制器
 */
defined('INHkMvc') or exit('Access Invalid!');

class TaskController extends Controller
{
    public function orderLockAct()
    {
        $lock = (int)$this->_getParam('lock', 0);
        $app = trim($this->_getParam('app', 'dekahome'));
        $lock_file = DEFAULT_UPLOAD_PATH . 'qrcode' . SEPARATE . $app . SEPARATE . 'order.lock';
        if (!file_exists(dirname($lock_file))) {
            makeDir(dirname($lock_file), 0777, 1);
        }
        file_put_contents($lock_file, $lock);
        if ($lock == file_get_contents($lock_file)) {
            echo 1;
            exit;
        } else {
            echo 0;
            exit;
        }
    }

    public function tbLockAct()
    {
        $lock = (int)$this->_getParam('lock', 0);
        $app = trim($this->_getParam('app', 'dekahome'));
        $lock_file = DEFAULT_UPLOAD_PATH . 'qrcode' . SEPARATE . $app . SEPARATE . 'tb.lock';
        if (!file_exists(dirname($lock_file))) {
            makeDir(dirname($lock_file), 0777, 1);
        }
        file_put_contents($lock_file, $lock);
        if ($lock == file_get_contents($lock_file)) {
            echo 1;
            exit;
        } else {
            echo 0;
            exit;
        }
    }

    public function adzoneAct()
    {
        $app = $_SESSION['argv'][3];
        $site_id = $_SESSION['argv'][4];
        $limit = $_SESSION['argv'][5];
        $mod = M('app.ad_zone');
        echo "site name: " . $app . " ***** site id: " . $site_id . " ***** limit: " . $limit . " ===>start" . PHP_EOL;
        for ($i = 1; $i <= $limit; $i++) {
            $data = taobaoCurl("adzone", ['to_page' => $i]);
            if ($i % 10 == 0) {
                sleep(60);
            }
            if ($data == 302) {
                $j = 0;
                while ($data == 302 && $j < 3) {
                    $data = taobaoCurl("adzone", ['to_page' => $i]);
                    $j++;
                    sleep($j * 60);
                }
                echo "to page " . $i . " ===>302" . PHP_EOL;
            }
            echo "to page " . $i . " ===>begin" . PHP_EOL;
            if ($data['data']['pagelist'] == null) {
                echo "to page " . $i . " ===>end" . PHP_EOL;
                break;
            }
            foreach ($data['data']['pagelist'] as $v) {

                if ($v['siteid'] != '18564365' && $app == "dekahome") {
                    continue;
                }

                $data = [
                    'ad_zone_id' => $v['adzoneid'],
                    'admin_uid' => 0,
                    'add_time' => time(),
                    'status' => 0,
                    'member_id' => 0,
                    'site_name' => $app,
                    'site_id' => $site_id,
                ];
                $res = $mod->add($data);
                if ($res === "exist") {
                    echo $v['adzoneid'] . " => exist" . PHP_EOL;
                } elseif (is_numeric($res)) {

                } else {
                    echo $v['adzoneid'] . " => failed" . PHP_EOL;
                }
            }
        }
    }

}
